package com.rememberme;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckedTextView;
import android.widget.TextView;

import com.business.ReminderBus;
import com.dataaccess.MyDatabase;
import com.dataobject.Group;
import com.dataobject.MyReminder;

public class MyExpandableListAdapter extends BaseExpandableListAdapter {
	private final SparseArray<Group> _Groups;
	private LayoutInflater _Inf;
	private Activity _Act;
	private ReminderBus _Bus;

	public MyExpandableListAdapter(Activity act, SparseArray<Group> groups,
			MyDatabase myDB) {
		_Act = act;
		_Groups = groups;
		_Inf = act.getLayoutInflater();
		_Bus = new ReminderBus(act);
	}

	@Override
	public Object getChild(int groupPos, int childPos) {
		return _Groups.get(groupPos).get_SubItems().get(childPos);
	}

	@Override
	public long getChildId(int arg0, int arg1) {
		return 0;
	}

	@Override
	public View getChildView(final int groupPos, final int childPos,
			boolean isLastChild, View convertView, ViewGroup parent) {
		final MyReminder children = (MyReminder) getChild(groupPos, childPos);
		TextView txt = null;
		if (convertView == null) {
			convertView = _Inf.inflate(R.layout.remind_list_item, null);
		}
		txt = (TextView) convertView.findViewById(R.id.txtTitle);
		txt.setText(children.get_Title());
		txt.setCompoundDrawablesWithIntrinsicBounds(
				getDrawableByName(children.get_Ico()), null, null, null);
		convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Dialog dialog = initContextMenu(children, groupPos, childPos);
				dialog.show();
			}
		});
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPos) {
		return _Groups.get(groupPos).get_SubItems().size();
	}

	@Override
	public Object getGroup(int groupPos) {
		return _Groups.get(groupPos);
	}

	@Override
	public int getGroupCount() {
		return _Groups.size();
	}

	@Override
	public long getGroupId(int groupPos) {
		return 0;
	}

	@Override
	public View getGroupView(int groupPos, boolean isExpanded,
			View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = _Inf.inflate(R.layout.remind_list_group, null);
		}
		Group group = (Group) getGroup(groupPos);
		((CheckedTextView) convertView).setText(group.get_DatePeriod()
				+ group.get_ViewSize());
		((CheckedTextView) convertView).setChecked(isExpanded);
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		return false;
	}

	@Override
	public void onGroupCollapsed(int groupPos) {
		super.onGroupCollapsed(groupPos);
	}

	@Override
	public void onGroupExpanded(int groupPos) {
		super.onGroupExpanded(groupPos);
	}

	private Drawable getDrawableByName(String name) {
		return _Act.getResources().getDrawable(
				_Act.getResources().getIdentifier(name, "drawable",
						_Act.getPackageName()));
	}

	public Dialog initContextMenu(final MyReminder mr, final int groupPos,
			int childPos) {
		AlertDialog.Builder builder = new Builder(_Act);
		final String[] context_arr = _Act.getResources().getStringArray(
				R.array.context_arr);
		builder.setTitle(_Act.getResources().getString(R.string.context_select))
				.setItems(context_arr, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int pos) {
						dialog.dismiss();
						switch (pos) {
						// ---------- ---------- ----------
						// VIEW / EDIT
						// ---------- ---------- ----------
						case 0: {
							Intent intent = new Intent(_Act, RemindEditor.class);
							intent.putExtra("isNew", false);
							intent.putExtra("id", mr.get_Id());
							intent.putExtra("ico", mr.get_Ico());
							intent.putExtra("title", mr.get_Title());
							intent.putExtra("content", mr.get_Content());
							intent.putExtra("datetime", mr.get_DateTime()
									.getTimeInMillis());
							intent.putExtra("alarm", mr.get_Alarm());
							intent.putExtra("tone", mr.get_Tone());
							_Act.startActivity(intent);

							break;
						}

						// ---------- ---------- ----------
						// ADD
						// ---------- ---------- ----------
						case 1: {
							Intent intent = new Intent(_Act, RemindEditor.class);
							intent.putExtra("isNew", true);
							_Act.startActivity(intent);
							break;
						}
						// ---------- ---------- ----------
						// DELETE
						// ---------- ---------- ----------
						case 2: {
							_Bus.deleteReminder(mr);
							_Groups.get(groupPos).get_SubItems().remove(mr);
							_Groups.get(groupPos).set_ViewSize(
									"("
											+ _Groups.get(groupPos)
													.get_SubItems().size()
											+ ")");
							MyExpandableListAdapter.this.notifyDataSetChanged();
							break;
						}
						// ---------- ---------- ----------
						// DELETE ALL
						// ---------- ---------- ----------
						case 3: {
							_Bus.deleteAll();
							for (int i = 0; i < _Groups.size(); i++) {
								_Groups.get(i).get_SubItems().clear();
								_Groups.get(i).set_ViewSize("(0)");
								MyExpandableListAdapter.this
										.notifyDataSetChanged();
							}
							break;
						}
						}
					}
				});
		return builder.create();
	}
}
